package com.qixin.api.serviceimpl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.qixin.api.bean.PtRole;
import com.qixin.api.bean.PtRoleMenu;
import com.qixin.api.bean.ResultPage;
import com.qixin.api.bean.VoPage;
import com.qixin.api.mapper.PtRoleMapper;
import com.qixin.api.service.PtRoleMenuService;
import com.qixin.api.service.PtRoleService;
import com.qixin.api.service.PtRoleUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.*;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author 吕正睿
 * @since 2018-11-13
 */
@Service
public class PtRoleServiceImpl extends ServiceImpl<PtRoleMapper, PtRole> implements PtRoleService {
@Autowired
PtRoleMenuService ptRoleMenuService;
@Autowired
PtRoleUserService ptRoleUserService;
	@Override
	public ResultPage<PtRole> selectMyPage(VoPage voPage, PtRole ptRole) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("page", (voPage.getPage() - 1)*voPage.getLimit());
		paramMap.put("limit", voPage.getLimit());
		List<PtRole> list = baseMapper.selectMyPage(paramMap,ptRole);
		Integer count = baseMapper.countMyPage(paramMap,ptRole);
		ResultPage<PtRole>resultPage=new ResultPage<>();
		resultPage.setCode(0);
		resultPage.setCount(count);
		resultPage.setMsg("成功");
		resultPage.setData(list);
		return resultPage;
	}

		@Override
		@Transactional
		public boolean insertRoleAndMenu(String param) {
			//把传过来的json字符串转成Map集合
			Map mapObj = JSONObject.parseObject(param,Map.class);
			PtRole ptRole=new PtRole();
			//获取map中 填入的角色名字
			ptRole.setRoleName(mapObj.get("roleName")+"");
			ptRole.setCreateTime(new Date().getTime());
			//该角色唯一标识
			ptRole.setRoleValue(UUID.randomUUID()+"");
			//如果新增角色成功
			if(this.insert(ptRole)){
				//获取map中所有选中的菜单id
				String menuId=(String) mapObj.get("menuId");
				//分割成独立的id
				String[] strs=menuId.split(",");
				//循环遍历数组中的所有菜单ID
				for (int i = 0; i < strs.length; i++) {
					boolean flag=false;
					PtRoleMenu ptRoleMenu=new PtRoleMenu();
					ptRoleMenu.setmId(Long.parseLong(strs[i]));
					ptRoleMenu.setrId(ptRole.getId());
					//保存角色和菜单关联表
					if( ptRoleMenuService.insert(ptRoleMenu)){
						flag=true;
					} else{
						flag=false;
					}
					//如果flag是假  就返回false
					if (!flag){
						return  false;
					}
				}
			}
			return  true;

		}


	@Override
	@Transactional(rollbackFor = {SQLException.class})
	public String deleteRole(Long id) throws SQLException {
		PtRole ptRole=new PtRole();
		ptRole.setId(id);
		if(this.deleteById(ptRole)){
			Map map=new HashMap<>();
			map.put("role_id",ptRole.getId());
			ptRoleUserService.deleteByMap(map);
			Map map1=new HashMap<>();
			map1.put("r_id",ptRole.getId());
			if(ptRoleMenuService.deleteByMap(map1)){
				return  "删除成功";
			}


		}
		throw   new  SQLException();
	}
}
